<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    MySQL 中的 21 个好习惯你要养成.md |  
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-mysql/MySQL 中的 21 个好习惯你要养成"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  MySQL 中的 21 个好习惯你要养成.md
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/11/11/mysql/MySQL%20%E4%B8%AD%E7%9A%84%2021%20%E4%B8%AA%E5%A5%BD%E4%B9%A0%E6%83%AF%E4%BD%A0%E8%A6%81%E5%85%BB%E6%88%90/" class="article-date">
  <time datetime="2020-11-10T16:00:00.000Z" itemprop="datePublished">2020-11-11</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/mysql/">mysql</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2.5k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">10 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="MySQL-中的-21-个好习惯你要养成"><a href="#MySQL-中的-21-个好习惯你要养成" class="headerlink" title="MySQL 中的 21 个好习惯你要养成"></a>MySQL 中的 21 个好习惯你要养成</h1><p>前言</p>
<p>每一个好习惯都是一笔财富，本文分SQL后悔药， SQL性能优化，SQL规范优雅三个方向，分享写SQL的21个好习惯，谢谢阅读，加油哈~</p>
<h4 id="1-写完SQL先explain查看执行计划（SQL性能优化）"><a href="#1-写完SQL先explain查看执行计划（SQL性能优化）" class="headerlink" title="1. 写完SQL先explain查看执行计划（SQL性能优化）"></a>1. 写完SQL先explain查看执行计划（SQL性能优化）</h4><p>日常开发写SQL的时候，尽量养成这个好习惯呀：写完SQL后，用explain分析一下，尤其注意走不走索引。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">explain select userid,name,age from user   </span><br><span class="line">where userid &#x3D;10086 or age &#x3D;18;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="2、操作delete或者update语句，加个limit-SQL后悔药）"><a href="#2、操作delete或者update语句，加个limit-SQL后悔药）" class="headerlink" title="2、操作delete或者update语句，加个limit(SQL后悔药）"></a>2、操作delete或者update语句，加个limit(SQL后悔药）</h4><p>在执行删除或者更新语句，尽量加上limit，以下面的这条 SQL 为例吧：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">delete from euser where age &gt; 30 limit 200;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>因为加了limit 主要有这些好处：<img src=""></p>
<ul>
<li><p><strong>「降低写错SQL的代价」</strong>, 你在命令行执行这个SQL的时候，如果不加limit，执行的时候一个<strong>「不小心手抖」</strong>，可能数据全删掉了，如果<strong>「删错」</strong>了呢？加了limit 200，就不一样了。删错也只是丢失200条数据，可以通过binlog日志快速恢复的。</p>
</li>
<li><p><strong>「SQL效率很可能更高」</strong>，你在SQL行中，加了limit 1，如果第一条就命中目标return， 没有limit的话，还会继续执行扫描表。</p>
</li>
<li><p><strong>「避免了长事务」</strong>，delete执行时,如果age加了索引，MySQL会将所有相关的行加写锁和间隙锁，所有执行相关行会被锁住，如果删除数量大，会直接影响相关业务无法使用。</p>
</li>
<li><p><strong>「数据量大的话，容易把CPU打满」</strong> ,如果你删除数据量很大时，不加 limit限制一下记录数，容易把cpu打满，导致越删越慢的。</p>
</li>
</ul>
<h4 id="3-设计表的时候，所有表和字段都添加相应的注释（SQL规范优雅）"><a href="#3-设计表的时候，所有表和字段都添加相应的注释（SQL规范优雅）" class="headerlink" title="3. 设计表的时候，所有表和字段都添加相应的注释（SQL规范优雅）"></a>3. 设计表的时候，所有表和字段都添加相应的注释（SQL规范优雅）</h4><p>这个好习惯一定要养成啦，设计数据库表的时候，所有表和字段都添加相应的注释，后面更容易维护。</p>
<p><strong>「正例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">CREATE TABLE &#96;account&#96; (  </span><br><span class="line">  &#96;id&#96; int(11) NOT NULL AUTO_INCREMENT COMMENT &#39;主键Id&#39;,  </span><br><span class="line">  &#96;name&#96; varchar(255) DEFAULT NULL COMMENT &#39;账户名&#39;,  </span><br><span class="line">  &#96;balance&#96; int(11) DEFAULT NULL COMMENT &#39;余额&#39;,  </span><br><span class="line">  &#96;create_time&#96; datetime NOT NULL COMMENT &#39;创建时间&#39;,  </span><br><span class="line">  &#96;update_time&#96; datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT &#39;更新时间&#39;,  </span><br><span class="line">  PRIMARY KEY (&#96;id&#96;),  </span><br><span class="line">  KEY &#96;idx_name&#96; (&#96;name&#96;) USING BTREE  </span><br><span class="line">) ENGINE&#x3D;InnoDB AUTO_INCREMENT&#x3D;1570068 DEFAULT CHARSET&#x3D;utf8 ROW_FORMAT&#x3D;REDUNDANT COMMENT&#x3D;&#39;账户表&#39;;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>「反例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">CREATE TABLE &#96;account&#96; (  </span><br><span class="line">  &#96;id&#96; int(11) NOT NULL AUTO_INCREMENT,  </span><br><span class="line">  &#96;name&#96; varchar(255) DEFAULT NULL,  </span><br><span class="line">  &#96;balance&#96; int(11) DEFAULT NULL,  </span><br><span class="line">  &#96;create_time&#96; datetime NOT NULL ,  </span><br><span class="line">  &#96;update_time&#96; datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP,  </span><br><span class="line">  PRIMARY KEY (&#96;id&#96;),  </span><br><span class="line">  KEY &#96;idx_name&#96; (&#96;name&#96;) USING BTREE  </span><br><span class="line">) ENGINE&#x3D;InnoDB AUTO_INCREMENT&#x3D;1570068 DEFAULT CHARSET&#x3D;utf8;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="4-SQL书写格式，关键字大小保持一致，使用缩进。（SQL规范优雅）"><a href="#4-SQL书写格式，关键字大小保持一致，使用缩进。（SQL规范优雅）" class="headerlink" title="4. SQL书写格式，关键字大小保持一致，使用缩进。（SQL规范优雅）"></a>4. SQL书写格式，关键字大小保持一致，使用缩进。（SQL规范优雅）</h4><p><strong>「正例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">SELECT stu.name, sum(stu.score)  </span><br><span class="line">FROM Student stu  </span><br><span class="line">WHERE stu.classNo &#x3D; &#39;1班&#39;  </span><br><span class="line">GROUP BY stu.name  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>「反例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">SELECT stu.name, sum(stu.score) from Student stu WHERE stu.classNo &#x3D; &#39;1班&#39; group by stu.name.  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>显然，统一关键字大小写一致，使用缩进对齐，会使你的SQL看起来更优雅~</p>
<h4 id="5-INSERT语句标明对应的字段名称（SQL规范优雅）"><a href="#5-INSERT语句标明对应的字段名称（SQL规范优雅）" class="headerlink" title="5. INSERT语句标明对应的字段名称（SQL规范优雅）"></a>5. INSERT语句标明对应的字段名称（SQL规范优雅）</h4><p><strong>「反例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">insert into Student values (&#39;666&#39;,&#39;捡田螺的小男孩&#39;,&#39;100&#39;);  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>「正例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">insert into Student(student_id,name,score) values (&#39;666&#39;,&#39;捡田螺的小男孩&#39;,&#39;100&#39;);  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="6-变更SQL操作先在测试环境执行，写明详细的操作步骤以及回滚方案，并在上生产前review。（SQL后悔药）"><a href="#6-变更SQL操作先在测试环境执行，写明详细的操作步骤以及回滚方案，并在上生产前review。（SQL后悔药）" class="headerlink" title="6. 变更SQL操作先在测试环境执行，写明详细的操作步骤以及回滚方案，并在上生产前review。（SQL后悔药）"></a>6. 变更SQL操作先在测试环境执行，写明详细的操作步骤以及回滚方案，并在上生产前review。（SQL后悔药）</h4><ul>
<li><p>变更SQL操作先在测试环境测试，避免有语法错误就放到生产上了。</p>
</li>
<li><p>变更Sql操作需要写明详细操作步骤，尤其有依赖关系的时候，如：先修改表结构再补充对应的数据。</p>
</li>
<li><p>变更Sql操作有回滚方案，并在上生产前，review对应变更SQL。</p>
</li>
</ul>
<h4 id="7-设计数据库表的时候，加上三个字段：主键，create-time-update-time。（SQL规范优雅）"><a href="#7-设计数据库表的时候，加上三个字段：主键，create-time-update-time。（SQL规范优雅）" class="headerlink" title="7.设计数据库表的时候，加上三个字段：主键，create_time,update_time。（SQL规范优雅）"></a>7.设计数据库表的时候，加上三个字段：主键，create_time,update_time。（SQL规范优雅）</h4><p><strong>「反例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">CREATE TABLE &#96;account&#96; (  </span><br><span class="line">  &#96;name&#96; varchar(255) DEFAULT NULL COMMENT &#39;账户名&#39;,  </span><br><span class="line">  &#96;balance&#96; int(11) DEFAULT NULL COMMENT &#39;余额&#39;,  </span><br><span class="line">) ENGINE&#x3D;InnoDB AUTO_INCREMENT&#x3D;1570068 DEFAULT CHARSET&#x3D;utf8 ROW_FORMAT&#x3D;REDUNDANT COMMENT&#x3D;&#39;账户表&#39;;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>「正例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">CREATE TABLE &#96;account&#96; (  </span><br><span class="line">  &#96;id&#96; int(11) NOT NULL AUTO_INCREMENT COMMENT &#39;主键Id&#39;,  </span><br><span class="line">  &#96;name&#96; varchar(255) DEFAULT NULL COMMENT &#39;账户名&#39;,  </span><br><span class="line">  &#96;balance&#96; int(11) DEFAULT NULL COMMENT &#39;余额&#39;,  </span><br><span class="line">  &#96;create_time&#96; datetime NOT NULL COMMENT &#39;创建时间&#39;,  </span><br><span class="line">  &#96;update_time&#96; datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT &#39;更新时间&#39;,  </span><br><span class="line">  PRIMARY KEY (&#96;id&#96;),  </span><br><span class="line">  KEY &#96;idx_name&#96; (&#96;name&#96;) USING BTREE  </span><br><span class="line">) ENGINE&#x3D;InnoDB AUTO_INCREMENT&#x3D;1570068 DEFAULT CHARSET&#x3D;utf8 ROW_FORMAT&#x3D;REDUNDANT COMMENT&#x3D;&#39;账户表&#39;;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>「理由：」</strong></p>
<ul>
<li><p>主键一般都要加上的，没有主键的表是没有灵魂的</p>
</li>
<li><p>创建时间和更新时间的话，还是建议加上吧，详细审计、跟踪记录，都是有用的。</p>
</li>
</ul>
<p>阿里开发手册也提到这个点，如图</p>
<p><img src=""></p>
<h4 id="8-写完SQL语句，检查where-order-by-group-by后面的列，多表关联的列是否已加索引，优先考虑组合索引。（SQL性能优化）"><a href="#8-写完SQL语句，检查where-order-by-group-by后面的列，多表关联的列是否已加索引，优先考虑组合索引。（SQL性能优化）" class="headerlink" title="8. 写完SQL语句，检查where,order by,group by后面的列，多表关联的列是否已加索引，优先考虑组合索引。（SQL性能优化）"></a>8. 写完SQL语句，检查where,order by,group by后面的列，多表关联的列是否已加索引，优先考虑组合索引。（SQL性能优化）</h4><p><strong>「反例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select * from user   </span><br><span class="line">where address &#x3D;&#39;深圳&#39; order by age;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><img src=""></p>
<p><strong>「正例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">添加索引  </span><br><span class="line">alter table user add index idx_address_age (address,age)  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><img src=""></p>
<h4 id="9-修改或删除重要数据前，要先备份，先备份，先备份（SQL后悔药）"><a href="#9-修改或删除重要数据前，要先备份，先备份，先备份（SQL后悔药）" class="headerlink" title="9.修改或删除重要数据前，要先备份，先备份，先备份（SQL后悔药）"></a>9.修改或删除重要数据前，要先备份，先备份，先备份（SQL后悔药）</h4><p>如果要修改或删除数据，在执行SQL前一定要先备份要修改的数据，万一误操作，还能吃口<strong>「后悔药」</strong>~</p>
<h4 id="10-where后面的字段，留意其数据类型的隐式转换（SQL性能优化）"><a href="#10-where后面的字段，留意其数据类型的隐式转换（SQL性能优化）" class="headerlink" title="10. where后面的字段，留意其数据类型的隐式转换（SQL性能优化）"></a>10. where后面的字段，留意其数据类型的隐式转换（SQL性能优化）</h4><p><strong>「反例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;userid 是varchar字符串类型  </span><br><span class="line">select * from user where userid &#x3D;123;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><img src=""></p>
<p><strong>「正例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">select * from user where userid &#x3D;&#39;123&#39;;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><img src=""></p>
<p><strong>「理由：」</strong></p>
<ul>
<li>因为不加单引号时，是字符串跟数字的比较，它们类型不匹配，MySQL会做隐式的类型转换，把它们转换为浮点数再做比较，最后导致索引失效</li>
</ul>
<h4 id="11-尽量把所有列定义为NOT-NULL（SQL规范优雅）"><a href="#11-尽量把所有列定义为NOT-NULL（SQL规范优雅）" class="headerlink" title="11. 尽量把所有列定义为NOT NULL（SQL规范优雅）"></a>11. 尽量把所有列定义为NOT NULL（SQL规范优雅）</h4><ul>
<li><p><strong>「NOT NULL列更节省空间」</strong>，NULL列需要一个额外字节作为判断是否为 NULL 的标志位。</p>
</li>
<li><p><strong>「NULL列需要注意空指针问题」</strong>，NULL列在计算和比较的时候，需要注意空指针问题。</p>
</li>
</ul>
<h4 id="12-修改或者删除SQL，先写WHERE查一下，确认后再补充-delete-或-update（SQL后悔药）"><a href="#12-修改或者删除SQL，先写WHERE查一下，确认后再补充-delete-或-update（SQL后悔药）" class="headerlink" title="12.修改或者删除SQL，先写WHERE查一下，确认后再补充 delete 或 update（SQL后悔药）"></a>12.修改或者删除SQL，先写WHERE查一下，确认后再补充 delete 或 update（SQL后悔药）</h4><p>尤其在操作生产的数据时，遇到修改或者删除的SQL，先加个where查询一下，确认OK之后，再执行update或者delete操作</p>
<h4 id="13-减少不必要的字段返回，如使用select-lt-具体字段-gt-代替-select-（SQL性能优化）"><a href="#13-减少不必要的字段返回，如使用select-lt-具体字段-gt-代替-select-（SQL性能优化）" class="headerlink" title="13.减少不必要的字段返回，如使用select &lt;具体字段&gt; 代替 select * （SQL性能优化）"></a>13.减少不必要的字段返回，如使用select &lt;具体字段&gt; 代替 select * （SQL性能优化）</h4><p><strong>「反例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">select * from employee;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>「正例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">select id，name from employee;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>理由：</p>
<ul>
<li><p>节省资源、减少网络开销。</p>
</li>
<li><p>可能用到覆盖索引，减少回表，提高查询效率。</p>
</li>
</ul>
<h4 id="14-所有表必须使用Innodb存储引擎（SQL规范优雅）"><a href="#14-所有表必须使用Innodb存储引擎（SQL规范优雅）" class="headerlink" title="14.所有表必须使用Innodb存储引擎（SQL规范优雅）"></a>14.所有表必须使用Innodb存储引擎（SQL规范优雅）</h4><p>Innodb <strong>「支持事务，支持行级锁，更好的恢复性」</strong>，高并发下性能更好，所以呢，没有特殊要求（即Innodb无法满足的功能如：列存储，存储空间数据等）的情况下，所有表必须使用Innodb存储引擎</p>
<h4 id="15-数据库和表的字符集尽量统一使用UTF8（SQL规范优雅）"><a href="#15-数据库和表的字符集尽量统一使用UTF8（SQL规范优雅）" class="headerlink" title="15.数据库和表的字符集尽量统一使用UTF8（SQL规范优雅）"></a>15.数据库和表的字符集尽量统一使用UTF8（SQL规范优雅）</h4><p>尽量统一使用UTF8编码</p>
<ul>
<li><p>可以避免乱码问题</p>
</li>
<li><p>可以避免，不同字符集比较转换，导致的索引失效问题</p>
</li>
</ul>
<p><strong>「如果需要存储表情，那么选择utf8mb4来进行存储，注意它与utf-8编码的区别。」</strong></p>
<h4 id="16-尽量使用varchar代替-char。（SQL性能优化）"><a href="#16-尽量使用varchar代替-char。（SQL性能优化）" class="headerlink" title="16. 尽量使用varchar代替 char。（SQL性能优化）"></a>16. 尽量使用varchar代替 char。（SQL性能优化）</h4><p><strong>「反例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#96;deptName&#96; char(100) DEFAULT NULL COMMENT &#39;部门名称&#39;</span><br></pre></td></tr></table></figure>

<p><strong>「正例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&#96;deptName&#96; varchar(100) DEFAULT NULL COMMENT &#39;部门名称&#39;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>理由：</p>
<ul>
<li>因为首先变长字段存储空间小，可以节省存储空间。</li>
</ul>
<h4 id="17-如果修改字段含义或对字段表示的状态追加时，需要及时更新字段注释。（SQL规范优雅）"><a href="#17-如果修改字段含义或对字段表示的状态追加时，需要及时更新字段注释。（SQL规范优雅）" class="headerlink" title="17. 如果修改字段含义或对字段表示的状态追加时，需要及时更新字段注释。（SQL规范优雅）"></a>17. 如果修改字段含义或对字段表示的状态追加时，需要及时更新字段注释。（SQL规范优雅）</h4><p>这个点，是阿里开发手册中，Mysql的规约。你的字段，尤其是表示枚举状态时，如果含义被修改了，或者状态追加时，为了后面更好维护，需要即时更新字段的注释。</p>
<h4 id="18-SQL命令行修改数据，养成begin-commit-事务的习惯-SQL后悔药"><a href="#18-SQL命令行修改数据，养成begin-commit-事务的习惯-SQL后悔药" class="headerlink" title="18. SQL命令行修改数据，养成begin + commit 事务的习惯(SQL后悔药)"></a>18. SQL命令行修改数据，养成begin + commit 事务的习惯(SQL后悔药)</h4><p><strong>「正例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">begin;  </span><br><span class="line">update account set balance &#x3D;1000000  </span><br><span class="line">where name &#x3D;&#39;捡田螺的小男孩&#39;;  </span><br><span class="line">commit;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>「反例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">update account set balance &#x3D;1000000  </span><br><span class="line">where name &#x3D;&#39;捡田螺的小男孩&#39;;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="19-索引命名要规范，主键索引名为-pk-字段名；唯一索引名为-uk-字段名-；普通索引名则为-idx-字段名。（SQL规范优雅）"><a href="#19-索引命名要规范，主键索引名为-pk-字段名；唯一索引名为-uk-字段名-；普通索引名则为-idx-字段名。（SQL规范优雅）" class="headerlink" title="19. 索引命名要规范，主键索引名为 pk_ 字段名；唯一索引名为 uk _字段名 ；普通索引名则为 idx _字段名。（SQL规范优雅）"></a>19. 索引命名要规范，主键索引名为 pk_ 字段名；唯一索引名为 uk _字段名 ；普通索引名则为 idx _字段名。（SQL规范优雅）</h4><p>说明：pk_即primary key；uk_即unique key；idx_即index 的简称。</p>
<h4 id="20-WHERE从句中不对列进行函数转换和表达式计算"><a href="#20-WHERE从句中不对列进行函数转换和表达式计算" class="headerlink" title="20. WHERE从句中不对列进行函数转换和表达式计算"></a>20. WHERE从句中不对列进行函数转换和表达式计算</h4><p>假设loginTime加了索引</p>
<p><strong>「反例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">select userId,loginTime   </span><br><span class="line">from loginuser  </span><br><span class="line">where Date_ADD(loginTime,Interval 7 DAY) &gt;&#x3D;now();  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>「正例：」</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">explain  select userId,loginTime   </span><br><span class="line">from loginuser   </span><br><span class="line">where  loginTime &gt;&#x3D; Date_ADD(NOW(),INTERVAL - 7 DAY);  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>「理由：」</strong></p>
<ul>
<li>索引列上使用mysql的内置函数，索引失效</li>
</ul>
<p><img src=""></p>
<h4 id="21-如果修改-更新数据过多，考虑批量进行。"><a href="#21-如果修改-更新数据过多，考虑批量进行。" class="headerlink" title="21.如果修改/更新数据过多，考虑批量进行。"></a>21.如果修改/更新数据过多，考虑批量进行。</h4><p>反例：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">delete from account  limit 100000;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>正例：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">for each(200次)  </span><br><span class="line">&#123;  </span><br><span class="line"> delete from account  limit 500;  </span><br><span class="line">&#125;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>理由：</p>
<ul>
<li><p>大批量操作会会造成主从延迟。</p>
</li>
<li><p>大批量操作会产生大事务，阻塞。</p>
</li>
<li><p>大批量操作，数据量过大，会把cpu打满。</p>
</li>
</ul>
<h4 id="参考与感谢"><a href="#参考与感谢" class="headerlink" title="参考与感谢"></a>参考与感谢</h4><ul>
<li><p>[delete后加 limit是个好习惯么] (<a target="_blank" rel="noopener" href="https://blog.csdn.net/qq/_39390545/article/details/107519747">https://blog.csdn.net/qq\_39390545/article/details/107519747</a>)</p>
</li>
<li><p>《阿里开发手册》</p>
</li>
</ul>
<p>往期精选</p>
<p><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MzU2NDg0OTgyMA==&mid=2247495478&idx=1&sn=017e57562cb25b00a3392fe3aa584e3b&chksm=fc4612c5cb319bd353c4406a63e346b02ede8f7c04eabc07e23e52626e16af763378d7858b80&scene=21#wechat_redirect">看完这篇 final、finally 和 finalize 和面试官扯皮就没问题了</a></p>
<p><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MzU2NDg0OTgyMA==&mid=2247495431&idx=1&sn=78cfb914b224cfecf383614b9ad3090a&chksm=fc4612f4cb319be28313a8207b4437d8560eec0c1e8b473f0c9e45cae67758f47924bb061cef&scene=21#wechat_redirect">搞不懂啊！为啥计算机的本质竟然是哲学？</a>  </p>
<p><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MzU2NDg0OTgyMA==&mid=2247495320&idx=1&sn=e41c333daa185639e0f72c695e6df8d9&chksm=fc46136bcb319a7dccb46142ba04bf2c787877e7391e1f0ed95d1db11cb4050df717221d7cf1&scene=21#wechat_redirect">一个 SpringBoot 项目都有哪些东西？</a></p>
<p><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MzU2NDg0OTgyMA==&mid=2247495160&idx=2&sn=0400d4beb50f49da3e65ad120f798549&chksm=fc46100bcb31991dc01c13fc83a14c9cae0cbe7b45f07f9dee6559a3eb8a4a5e9aab715755a0&scene=21#wechat_redirect">计算机网络基础知识总结</a></p>
<p><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MzU2NDg0OTgyMA==&mid=2247495153&idx=1&sn=22bd1fffd1cf007ed89fd913e3fcbee1&chksm=fc461002cb3199148ea98ba910e2facc5e4126e0115d92d655f04e90396378ed908f8b26940e&scene=21#wechat_redirect">面试官的灵魂一击：你懂 MySQL 事务日志吗？</a>  </p>
<p><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MzU2NDg0OTgyMA==&mid=2247494838&idx=1&sn=35443e13265d9c43de738b6670accad3&chksm=fc461145cb319853ee07bb46f16ac466515401c951cde203d7dc10c791e66b335dcf238df391&scene=21#wechat_redirect">分布式锁用 Redis 还是 Zookeeper？</a>  </p>
<p><a target="_blank" rel="noopener" href="http://mp.weixin.qq.com/s?__biz=MzU2NDg0OTgyMA==&mid=2247494810&idx=1&sn=8d64a3ed217a528ea522b0a489df9bd3&chksm=fc461169cb31987fa604e153a9a3423fa437dc2ee382100c74b57bfecf90113f91dbc66ec005&scene=21#wechat_redirect">为什么我使用了索引，查询还是慢？</a></p>
<p>另外，我输出了 <code>六本</code> PDF，已免费提供下载，如下所示</p>
<p><img src=""></p>
<p><img src=""></p>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2020/11/11/mysql/MySQL%20%E4%B8%AD%E7%9A%84%2021%20%E4%B8%AA%E5%A5%BD%E4%B9%A0%E6%83%AF%E4%BD%A0%E8%A6%81%E5%85%BB%E6%88%90/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/mysql/" rel="tag">mysql</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2020/11/11/develop/Python%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E4%B8%8E%E4%BB%A3%E7%A0%81%E8%AE%B2%E8%A7%A3%EF%BC%81/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Python发送邮件基础知识与代码讲解！.md
          
        </div>
      </a>
    
    
      <a href="/2020/11/11/other/%E5%90%91%205%20%E5%B2%81%E5%B0%8F%E6%9C%8B%E5%8F%8B%E8%A7%A3%E9%87%8A%E4%BB%80%E4%B9%88%E6%98%AF%E5%BE%AE%E6%9C%8D%E5%8A%A1%20/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">向 5 岁小朋友解释什么是微服务 .md</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2020
        <i class="ri-heart-fill heart_icon"></i> TzWind
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Hexo"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" target="_blank" rel="noopener" href="http://www.baidu.com">百度</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2019/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>